package servlet;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pojo.Customer;
import pojo.Favourite;
import service.CustomerManager;
import service.impl.CustomerManagerFactory;
import verify.MyVerify;

@WebServlet("/update")
public class UpdateCustomerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private CustomerManager manager;
	private MyVerify myVerify;
	private DateFormat dateFormat;
       
    public UpdateCustomerServlet() {
        super();
        manager = CustomerManagerFactory.getInstance();
        myVerify = new MyVerify();
        dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String cid = request.getParameter("id");
		String verify = myVerify.verifyInteger(cid);
		if (!verify.equals(MyVerify.SUCCESS)) {
			response.sendRedirect("error.jsp?info="+verify);
		}
		int id = Integer.parseInt(cid);
		Customer customer = manager.loadCustomer(id);
		request.setAttribute("customer", customer);
		request.getRequestDispatcher("update.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//接参
				String cid = request.getParameter("id");
				String cname = request.getParameter("cname");
				String csex = request.getParameter("sex");
				String csal = request.getParameter("sal");
				String cbirth = request.getParameter("birth");
				String[] cfavs = request.getParameterValues("favs");
				System.out.println(csex);
				//校验
				String verify = verify(cid,cname, csal, cbirth, csex,cfavs);
				if (!verify.equals(MyVerify.SUCCESS)) {
					response.sendRedirect("error?info="+verify);
					return;
				}
				
				//类型转换
				int id = Integer.parseInt(cid);
				boolean sex = Boolean.parseBoolean(csex);
				double sal = Double.parseDouble(csal);
				Date birth = null;
				try {
					 birth  = dateFormat.parse(cbirth);
				} catch (ParseException e) {
					e.printStackTrace();
				}
				//封装
				Customer c = new Customer(id, cname, sal, sex, birth);
				int[] favs = new int[cfavs.length];
				List<Favourite> favourites = new ArrayList<>();
				for (int i = 0; i < cfavs.length; i++) {
					System.out.println(cfavs[i]);
					favs[i] = Integer.parseInt(cfavs[i]);
					Favourite favourite = new Favourite();
					favourite.setId(favs[i]);
					favourites.add(favourite);
				}
				c.setFavs(favourites);
				//调用业务
				int m = manager.updateCustomer(c);
				if (m>0) {
					//存入context
					request.setAttribute("c", c);
					//转发
					System.out.println("5555");
					response.sendRedirect("list");
				}else{
					System.out.println("6666");
					response.sendRedirect("error?info=insert fail");
				}
				
				
			}
			
			public String verify(String id,String uname,String sal,String birth,String sex,String[] favs){
				String verify = myVerify.verifyInteger(id);
				if (!MyVerify.SUCCESS.equals(verify)) {
					return "id"+verify;
				}
				verify = myVerify.verifyString(uname);
				if (!MyVerify.SUCCESS.equals(verify)) {
					return "cname"+verify;
				}
				verify = myVerify.verifyDouble(sal);
				if (!MyVerify.SUCCESS.equals(verify)) {
					return "sal"+verify;
				}
				
				verify = myVerify.verifyDate(birth);
				if (!MyVerify.SUCCESS.equals(verify)) {
					return "birth"+verify;
				}
				verify = myVerify.verifyBoolean(sex);
				if (!MyVerify.SUCCESS.equals(verify)) {
					return "sex"+verify;
				}
				for (int i = 0; i < favs.length; i++) {
					verify = myVerify.verifyInteger(favs[i]);
					if (!verify.equals(MyVerify.SUCCESS)) {
						return "favs "+i+verify;
					}
				}
				return MyVerify.SUCCESS;
				
			}

}
